#include "view.h"
#include <QtGui/QResizeEvent>
#include <QtWidgets/QGraphicsScene>
#include <QtCharts/QChart>
#include <QtCharts/QLineSeries>
#include <QtCharts/QSplineSeries>
#include <QtWidgets/QGraphicsTextItem>
#include "callout.h"
#include <QtGui/QMouseEvent>
#include <QValueAxis>
#include <math.h>
#include <QDebug>

View::View(QWidget *parent)
    : QChartView(parent),
//      m_coordX(nullptr),
//      m_coordY(nullptr),
      m_chart(nullptr),
      m_tooltip(nullptr)
{
    move = false;

    setDragMode(QGraphicsView::NoDrag);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// do not change function sequence
    initial_chart();
//    initial_coordXY();

    setRenderHint(QPainter::Antialiasing);
    scene()->addItem(m_chart);

    this->setMouseTracking(true);
}

View::~View(){
    delete xAxis;
    delete yAxis;
    delete m_tooltip;
    delete m_chart;
}

void View::plot(const QList<double>& x, const QList<double>& y){
    double xi{0},yi{0};
    max_x = 0; min_x = 1e10; max_y = 0; min_y=1e10;

    QList<QPointF> points;
    QPointF point(0,0);
    for(int i=0;i<x.size();++i)
    {
        xi = x.at(i);
        yi = y.at(i);

        if(xi<min_x) min_x = xi;
        if(xi>max_x) max_x = xi;
        if(yi<min_y) min_y = yi;
        if(yi>max_y) max_y = yi;

        point.setX(xi);
        point.setY(yi);
        points.append(point);
    }
    min_y = min_y-0.1*std::abs(min_y);
    max_y = max_y+0.1*std::abs(max_y);
    series->replace(points);
    xAxis->setRange(static_cast<int>(min_x),static_cast<int>(max_x));
    yAxis->setRange(min_y,max_y);
}

void View::replot(){
    m_chart->zoomReset();
    xAxis->setRange(min_x, max_x);
    yAxis->setRange(min_y, max_y);
}

//void View::initial_coordXY(){
//    m_coordX = new QGraphicsSimpleTextItem(m_chart);
//    m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height());
//    m_coordX->setText("X: ");
//    m_coordX->setFont(QFont("Times New Roman"));
//    m_coordY = new QGraphicsSimpleTextItem(m_chart);
//    m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height());
//    m_coordY->setText("Y: ");
//    m_coordY->setFont(QFont("Times New Roman"));
//}

void View::initial_chart(){
    // chart
    m_chart = new QChart;
    m_chart->legend()->hide();

    series = new QLineSeries;
    m_chart->addSeries(series);

    xAxis = new QValueAxis();
    yAxis = new QValueAxis();
    xAxis->setRange(0,9);
    yAxis->setRange(0,1);
    xAxis->setLabelFormat("%d");
    xAxis->setTickCount(10);
    yAxis->setTickCount(10);

    m_chart->addAxis(xAxis,Qt::AlignBottom);
    m_chart->addAxis(yAxis,Qt::AlignLeft);
    series->attachAxis(xAxis);
    series->attachAxis(yAxis);
    m_chart->setAcceptHoverEvents(true);

    set_font();

    m_chart->setTitle("Signal");
    xAxis->setTitleText("Pixels");
    yAxis->setTitleText("Voltage");

    m_chart->setMargins(QMargins(0,0,0,0));

    connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
}

void View::set_font(){
    QFont font("Times New Roman");
    m_chart->axes().at(0)->setTitleFont(font);
    m_chart->axes().at(1)->setTitleFont(font);
    m_chart->setFont(font);
    m_chart->setTitleFont(font);
    xAxis->setLabelsFont(font);
    yAxis->setLabelsFont(font);
}

void View::resizeEvent(QResizeEvent *event)
{
    if (scene()) {
        scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
         m_chart->resize(event->size());
//         m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height() - 20);
//         m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height() - 20);
         foreach (Callout *callout, m_callouts)
             callout->updateGeometry();
    }
    QGraphicsView::resizeEvent(event);
}

void View::mousePressEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton)
    {
        move = true;
        pos_before = event->pos();
    }
}

void View::mouseReleaseEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton)
        move = false;
    setCursor(Qt::ArrowCursor);
}

void View::mouseMoveEvent(QMouseEvent *event)
{
    if (move)
    {
        QPoint oDeltaPos = event->pos() - pos_before;
        m_chart->scroll(-oDeltaPos.x(), oDeltaPos.y());
        pos_before = event->pos();
        setCursor(Qt::OpenHandCursor);
    }else{
//        m_coordX->setText(QString("X: %1").arg(m_chart->mapToValue(event->pos()).x()));
//        m_coordY->setText(QString("Y: %1").arg(m_chart->mapToValue(event->pos()).y()));
        QGraphicsView::mouseMoveEvent(event);
    }
}

void View::wheelEvent(QWheelEvent *event){
    if(event->delta() > 0)
        m_chart->zoomIn();
    else
        m_chart->zoomOut();
}

void View::tooltip(QPointF point, bool state)
{
    if (m_tooltip == nullptr)
        m_tooltip = new Callout(m_chart);

    if (state) {
        m_tooltip->setText(QString("X: %1 \nY: %2 ").arg(point.x()).arg(point.y()));
        m_tooltip->setAnchor(point);
        m_tooltip->setZValue(11);
        m_tooltip->updateGeometry();
        m_tooltip->show();
    } else {
        m_tooltip->hide();
    }
}

void View::set_chart_theme(int theme,const QString& style){
    m_chart->setTheme(static_cast<QChart::ChartTheme>(theme));
    set_font();
    setStyleSheet(style);
}

void View::set_chart_theme(int theme){
    m_chart->setTheme(static_cast<QChart::ChartTheme>(theme));
    set_font();
}

//void View::set_coord_bgc(const QBrush& brush){
//    m_coordX->setBrush(brush);
//    m_coordY->setBrush(brush);
//}
